//
//  BookController.swift
//  ios_swift_demo1
//
//  Created by liutao on 2020/7/15.
//  Copyright © 2020 lt. All rights reserved.
//

import UIKit


class BookController: BasePageViewController {
    
    private var topList = [LBUTopModel]()
    
    private var rankKingList = [LBURankingModel]()
    
    private lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumLineSpacing = 10
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.allowsSelection = true
        collectionView.register(cellType: RankCollectionViewCell.self)
        collectionView.register(cellType: TopCollectionViewCell.self)
//        collectionView.uHead = URefreshHeader{[weak self] in self?.setupLoadData()}
//        collectionView.uEmpty = UEmptyView{[weak self] in self?.setupLoadData()}
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.\
        setupLoadData()
    }
    
    //请求数据
    private func setupLoadData(){
        BaseApiProvider.request(.cateList, model: LBUCateListModel.self) { (returnData) in
//            self.collectionView.uEmpty?.allowShow = true
            self.topList = returnData?.topList ?? []
            self.rankKingList = returnData?.rankingList ?? []
            self.collectionView.reloadData()
//            self.collectionView.uHead.endRefreshing()
        }
    }
    
    override func setupLayout() {
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view.snp.edges)
        }
    }
    
    override func configNavigationBar() {
        super.configNavigationBar()
    }
}

extension BookController :UICollectionViewDelegate ,UICollectionViewDataSource{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return topList.prefix(3).count
        }else{
            return rankKingList.count
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: TopCollectionViewCell.self)
            cell.model = topList[indexPath.row]
            return cell
        }else{
            let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: RankCollectionViewCell.self)
            cell.model = rankKingList[indexPath.row]
            return cell
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: section == 0 ? 0 : 10, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = floor(Double(screenWidth - 40.0) / 3.0)
        return CGSize(width: width, height: (indexPath.section == 0 ? 55 : (width * 0.75 + 30)))
    }
    //
    //       func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    //
    //           // mark -- 增加
    //           if indexPath.section == 0 {
    //                      let model = topList[indexPath.row]
    //                      var titles: [String] = []
    //                      var vcs: [UIViewController] = []
    //                      for tab in model.extra?.tabList ?? [] {
    //                          guard let tabTitle = tab.tabTitle else { continue }
    //                          titles.append(tabTitle)
    //                          vcs.append(LBUComicListController(argCon: tab.argCon,
    //                                                              argName: tab.argName,
    //                                                              argValue: tab.argValue))
    //                      }
    //                      let vc = LBUPageController(titles: titles, vcs: vcs, pageStyle: .topTabBar)
    //                      vc.title = model.sortName
    //                      navigationController?.pushViewController(vc, animated: true)
    //                  }
    //
    //                  if indexPath.section == 1 {
    //                      let model = rankList[indexPath.row]
    //                      let vc = LBUComicListController(argCon: model.argCon,
    //                                                        argName: model.argName,
    //                                                        argValue: model.argValue)
    //                      vc.title = model.sortName
    //                      navigationController?.pushViewController(vc, animated: true)
    //                  }
    //       }
    
    
}
